﻿using System;
using System.Collections.Generic;
using SharpSqlAnalyzer.nodes;
using SharpSqlAnalyzer.pp.processor.type.comm;
using SharpSqlAnalyzer.pp.utils;
using SharpSqlAnalyzer.stmt;

namespace SharpSqlAnalyzer.pp.stmtformatter.type.comm
{
	// Token: 0x02000203 RID: 515
	public class CreateTableStmtFormatter : AbstractStmtFormatter
	{
		// Token: 0x06001204 RID: 4612 RVA: 0x003178B7 File Offset: 0x00315AB7
		public virtual void addItemListProcessor(AbstractProcessor p)
		{
			if (this.itemListProcessors == null)
			{
				this.itemListProcessors = new List<AbstractProcessor>();
			}
			this.itemListProcessors.Add(p);
		}

		// Token: 0x06001205 RID: 4613 RVA: 0x003178D8 File Offset: 0x00315AD8
		public virtual void addConstraintListProcessor(AbstractProcessor p)
		{
			if (this.constraintListProcessors == null)
			{
				this.constraintListProcessors = new List<AbstractProcessor>();
			}
			this.constraintListProcessors.Add(p);
		}

		// Token: 0x06001206 RID: 4614 RVA: 0x003178FC File Offset: 0x00315AFC
		protected internal override void doFormat(TCustomSqlStatement stmtArg)
		{
			TCreateTableSqlStatement tcreateTableSqlStatement = stmtArg as TCreateTableSqlStatement;
			this.runProcessor<TCreateTableSqlStatement>(this.SpecialProcessors, tcreateTableSqlStatement);
			TColumnDefinitionList columnList = tcreateTableSqlStatement.ColumnList;
			if (columnList != null && columnList.size() > 0)
			{
				this.runProcessor<TColumnDefinitionList>(this.itemListProcessors, columnList);
				for (int i = 0; i < columnList.size(); i++)
				{
					TColumnDefinition column = columnList.getColumn(i);
					if (column != null && column.Datatype != null)
					{
						ExpressionProcessor.processTypeName(this.Option, column.Datatype);
					}
				}
			}
			TConstraintList tableConstraints = tcreateTableSqlStatement.TableConstraints;
			if (tableConstraints != null && tableConstraints.size() > 0)
			{
				this.runProcessor<TConstraintList>(this.constraintListProcessors, tableConstraints);
			}
			if (tcreateTableSqlStatement.SubQuery != null)
			{
				ExpressionProcessor.processParenthesesNodeInSubQuery(this.Option, tcreateTableSqlStatement.SubQuery, tcreateTableSqlStatement.SubQuery.startToken, tcreateTableSqlStatement.SubQuery.endToken);
			}
			if (tcreateTableSqlStatement.GSharpSqlParser != null && tcreateTableSqlStatement.GSharpSqlParser.DbVendor == EDbVendor.dbvhive)
			{
				int length = SourceTokenOperator.curIndentLenVT(tcreateTableSqlStatement.startToken);
				IList<TSourceToken> list = new List<TSourceToken>();
				if (tcreateTableSqlStatement.TableComment != null)
				{
					TSourceToken item = SourceTokenSearcher.backforwardSearchNotWhitespaceAndReturnToken(tcreateTableSqlStatement.TableComment.startToken, 5, "comment");
					list.Add(item);
				}
				if (tcreateTableSqlStatement.HiveRowFormat != null)
				{
					list.Add(tcreateTableSqlStatement.HiveRowFormat.startToken);
				}
				if (tcreateTableSqlStatement.HiveTablePartition != null)
				{
					list.Add(tcreateTableSqlStatement.HiveTablePartition.startToken);
				}
				if (tcreateTableSqlStatement.HiveTableBuckets != null)
				{
					list.Add(tcreateTableSqlStatement.HiveTableBuckets.startToken);
				}
				if (tcreateTableSqlStatement.HiveTableFileFormat != null)
				{
					list.Add(tcreateTableSqlStatement.HiveTableFileFormat.startToken);
				}
				if (tcreateTableSqlStatement.HiveTableProperties != null)
				{
					list.Add(tcreateTableSqlStatement.HiveTableProperties.startToken);
				}
				if (tcreateTableSqlStatement.HiveTableSkewed != null)
				{
					list.Add(tcreateTableSqlStatement.HiveTableSkewed.startToken);
				}
				for (int j = 0; j < list.Count; j++)
				{
					TSourceToken tsourceToken = list[j];
					SourceTokenOperator.removeWhitespaceAndReturnFromEnd(this.Option, tsourceToken);
					SourceTokenOperator.addBefore(this.Option, tsourceToken, SourceTokenOperator.createReturnSourceToken());
					SourceTokenOperator.addBefore(this.Option, tsourceToken, SourceTokenOperator.createWhitespaceSourceToken(length));
				}
			}
		}

		// Token: 0x040013D5 RID: 5077
		private IList<AbstractProcessor> itemListProcessors;

		// Token: 0x040013D6 RID: 5078
		private IList<AbstractProcessor> constraintListProcessors;
	}
}
